package com.example.TCP_measure;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

public class Throughput extends Thread{
    private static final String TAG = "Throughput";
    private static double throughPut=0.0;//用于记录总的吞吐量
    Socket socket;
    String IP;
    int port;
    Handler handler;
    public Throughput(String IPAddress, int PORT, Handler h){
        IP=IPAddress;
        port=PORT;
        handler=h;
    }

    @Override
    public void run(){
        if(handler!=null){
            Message message =new Message();
            message.obj=throughPut+"";
            handler.sendMessage(message);
            throughPut=0.0;
        }
        else try{
            socket=new Socket(IP,port);
            socket.setSoTimeout(3000);//超过3秒认为超时
            Log.d(TAG,"子线程socket建立！");
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));//用于读取服务器输入
            while(true){
                if(bufferedReader.readLine().equals("over")){
                    Log.d(TAG,"over");
                    String throughput=bufferedReader.readLine();
                    throughPut += Double.parseDouble(throughput);
                    Log.d(TAG,throughPut+"");
                    break;
                }
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
